package com.woniu.p2p.behindController;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.woniu.p2p.entity.AdminRecord;
import com.woniu.p2p.entity.OperationRecord;
import com.woniu.p2p.entity.UserRecord;
import com.woniu.p2p.service.SystemRecordService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
;
import java.util.Map;

@Controller
@RequestMapping("/record")
public class RecordController {

    @Autowired
    private SystemRecordService systemRecordService;

    @RequestMapping("/selectAdmin")
    public String selectAdmin(@RequestBody Map reqMap,Map map) throws ParseException {
        int pageNum = 1;
        int pageSize = 10;
        try{
            pageNum = (int) reqMap.get("pageNum");
            pageSize = (int) reqMap.get("pageSize");
        }catch (Exception e){
        }
        map.put("recordType",reqMap.get("recordType"));
        map.put("beginTime",toDate(reqMap.get("beginTime")));
        map.put("endTime",toDate(reqMap.get("endTime")));
        map.put("keyword",reqMap.get("keyword"));
        map.put("pageSize",pageSize);

        PageHelper.startPage(pageNum,pageSize);
        PageInfo<AdminRecord> adminRecordPageInfo = new PageInfo<>(systemRecordService.searchAdminRecord(reqMap));
        map.put("adminRecordList",adminRecordPageInfo);

        return "content/personLoginRecord";
    }

    @RequestMapping("/selectUser")
    public String selectUser(@RequestBody Map reqMap,Map map) throws ParseException {
        int pageNum = 1;
        int pageSize = 10;
        try{
            pageNum = (int) reqMap.get("pageNum");
            pageSize = (int) reqMap.get("pageSize");
        }catch (Exception e){
        }
        map.put("beginTime",toDate(reqMap.get("beginTime")));
        map.put("endTime",toDate(reqMap.get("endTime")));
        map.put("keyword",reqMap.get("keyword"));
        map.put("pageSize",pageSize);
        PageHelper.startPage(pageNum,pageSize);
        PageInfo<UserRecord> userRecordList = new PageInfo<>(systemRecordService.searchUserRecord(reqMap));



        map.put("userRecordList",userRecordList);
        return "content/userLoginRecord";
    }

    @RequestMapping("/selectSystem")
    public String selectSystem(@RequestBody Map reqMap,Map map) throws ParseException {
        System.out.println(reqMap.get("pageNum"));
        int pageNum = 1;
        int pageSize = 10;
        try{
            pageNum = (int) reqMap.get("pageNum");
            pageSize = (int) reqMap.get("pageSize");
        }catch (Exception e){
        }
        map.put("beginTime",toDate(reqMap.get("beginTime")));
        map.put("endTime",toDate(reqMap.get("endTime")));
        map.put("keyword",reqMap.get("keyword"));
        map.put("pageSize",pageSize);

        PageHelper.startPage(pageNum,pageSize);
        PageInfo<OperationRecord> systemRecordList = new PageInfo<>(systemRecordService.searchOperationRecord(reqMap));

        map.put("systemRecordList",systemRecordList);
        return "content/SystemRecord";
    }

    public Date toDate(Object object){
        try{
            String dateS = (String) object;
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            if(dateS != null && !"".equals(dateS)){
                try {
                    Date parse = simpleDateFormat.parse((String)dateS);
                    return parse;
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
            return null;
        }catch (Exception e){
            return null;
        }
    }
}
